<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用递归获取所有标签、文本和注释</title>
</head>
<body>
    <div>
        <ul>
            <li>
                <a href="">
                    <!-- 内容 -->
                    <p></p>
                    <p></p>
                </a>
            </li>
            <li>
                <a href="">
                    <p></p>
                    <p></p>
                </a>
            </li>
            <li>
                <a href="">
                    <p></p>
                    <p></p>
                </a>
            </li>
        </ul>
    </div>
    <script>
        function getAll(el,nodeType = 1)
        {
            const nodes = [];
            // 将节点的集合转化为数组
            Array.from(el.childNodes).map(node => {
                if(node.nodeType == nodeType) nodes.push(node);
                if(node.nodeType == 1) nodes.push(...getAll(node,nodeType));
            })
            return nodes;
        }

        console.log(getAll(document.body,1));
    </script>
</body>
</html>